package gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import utils.ServerConnection;

/**
 *
 */
public class DasIrcWindow extends JFrame
{
    private final AboutPanel _aboutPanel = new AboutPanel( this );
    private final ChatsPanel chatsPanel_ = new ChatsPanel();
    private final ConnectToServerPanel connectPanel_ =
            new ConnectToServerPanel( this );

    private JPanel _currentPanel = null;
    
    private final LinkedList<ServerConnection> servers_ =
            new LinkedList<ServerConnection>();

    public DasIrcWindow()
    {
        super( "dasIRC" );

        createMenu();

        setLayout( new BorderLayout() );

        setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        setSize( 800, 500 );
        setLocationRelativeTo( null );

        add( connectPanel_, BorderLayout.CENTER );
        _currentPanel = connectPanel_;

        setVisible( true );
    }

    private void createMenu()
    {
        JMenuBar bar = new JMenuBar();

        // File menu
        JMenu file = new JMenu( "File" );

        JMenuItem exit = new JMenuItem( "Exit" );
        exit.addActionListener( new ActionListener() {
            public void actionPerformed( ActionEvent e )
            {
                for( ServerConnection server : servers_ )
                    server.disconnect( "dasIRC uber alles" );
                System.exit( 0 );
            }
        });

        file.add( exit );

        // Connection menu
        JMenu connection = new JMenu( "Connections" );

        JMenuItem connect = new JMenuItem( "Connect to server..." );

        final DasIrcWindow window = this;
        connect.addActionListener( new ActionListener() {
            public void actionPerformed( ActionEvent e )
            {
                window.switchToConnectToServerPanel();
            }
        });

        connection.add( connect );
        
        // Help menu
        JMenu help = new JMenu( "Help" );
        
        JMenuItem about = new JMenuItem( "About" );
        about.addActionListener( new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                window.switchToAboutPanel();
            }
        });
        
        help.add( about );

        // Add the menus to the bar
        bar.add( file );
        bar.add( connection );
        bar.add( help );
        setJMenuBar( bar );
    }

    public final void switchToConnectToServerPanel()
    {
        remove( _currentPanel );
        add( connectPanel_, BorderLayout.CENTER );
        validate();
        repaint();
        _currentPanel = connectPanel_;
    }

    public final void switchToChatPanel()
    {
        remove( _currentPanel );
        add( chatsPanel_, BorderLayout.CENTER );
        validate();
        repaint();
        _currentPanel = chatsPanel_;
    }

    public final void switchToAboutPanel() {
        remove( _currentPanel );
        add( _aboutPanel, BorderLayout.CENTER );
        validate();
        repaint();
        _currentPanel = _aboutPanel;
    }

    public final void createConnection( String host, int port, String nick )
    {
        for( ServerConnection s : servers_ )
            if( s.getName().equalsIgnoreCase( host ) )
                return;

        ServerConnection server = new ServerConnection( host, port, nick, chatsPanel_ );
        servers_.add( server ); // TODO: Is this really needed?
        chatsPanel_.add( server );

        validate();
        repaint();
        
        server.connect();
    }
}
